package com.csi.bumblebee.controller;

import com.csi.bumblebee.domain.util.MyResult;
import com.csi.bumblebee.service.UserStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.csi.bumblebee.domain.util.StateCode.*;

/**
 * @author h
 */
@CrossOrigin
@RestController
@RequestMapping("util/redis")
public class UserStorageController {

    @Autowired
    private UserStorageService userStorageService;

    @PostMapping
    public MyResult<Boolean> setExpTimeInRedis(@RequestParam String userId, @RequestParam double day) {
        MyResult<Boolean> myResult = new MyResult<>(UPDATE_FAILURE, "更新失败(取redis的过期时间)");
        if(userStorageService.setExpTimeInRedis(userId, day)) {
            myResult.setData(true);
            myResult.setCode(UPDATE_SUCCESS);
            myResult.setMessage("更新成功(取redis的过期时间)");
        }
        return myResult;
    }

    @GetMapping
    public MyResult<Long> getExpTimeInRedis(@RequestParam String userId) {
        MyResult<Long> myResult = new MyResult<>(FIND_FAILURE, "查询失败(取redis的过期时间)");
        Long time = userStorageService.getExpTimeInRedis(userId);
        if(time != null && (time > 0 || time == -1)) {
            myResult.setData(time);
            myResult.setCode(FIND_SUCCESS);
            myResult.setMessage("查询成功(取redis的过期时间)");
        }
        return myResult;
    }

}
